package com.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ID生成器
 * 
 * @author dingxude com.base.KeyGenerator 2013-2-26
 */
public class KeyGenerator
{
	private static volatile KeyGenerator instance;

	private static Object lock = new Object();

	private KeyGenerator() throws SQLException
	{
	}

	public static KeyGenerator getInstance() throws SQLException
	{
		if (instance == null)
		{
			synchronized (lock)
			{
				instance = new KeyGenerator();
			}
		}
		return instance;
	}

	/**
	 * 获取18位ID
	 * 
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	public String getKeyId(Connection conn) throws SQLException
	{
		PreparedStatement pst = null;
		ResultSet rs = null;
		String key = "";
		try
		{
			pst = conn.prepareStatement("SELECT FN_SYSTEM_ID FROM DUAL");
			rs = pst.executeQuery();
			while (rs.next())
			{
				key = rs.getString(1);
			}
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
				if (pst != null)
				{
					pst.close();
				}
			}
			catch (SQLException e)
			{
			}
		}
		return key;
	}

	/**
	 * 获取任务主键
	 * 
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	public String getTaskId(Connection conn) throws SQLException
	{
		PreparedStatement pst = null;
		ResultSet rs = null;
		String key = "";
		try
		{
			pst = conn.prepareStatement("SELECT FN_TASK_ID FROM DUAL");
			rs = pst.executeQuery();
			while (rs.next())
			{
				key = rs.getString(1);
			}
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
				if (pst != null)
				{
					pst.close();
				}
			}
			catch (SQLException e)
			{
			}
		}
		return key;
	}

	/**
	 * 获取项目主键
	 * 
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	public String getProjectId(Connection conn) throws SQLException
	{
		PreparedStatement pst = null;
		ResultSet rs = null;
		String key = "";
		try
		{
			pst = conn.prepareStatement("SELECT FN_PROJECT_ID FROM DUAL");
			rs = pst.executeQuery();
			while (rs.next())
			{
				key = rs.getString(1);
			}
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
				if (pst != null)
				{
					pst.close();
				}
			}
			catch (SQLException e)
			{
			}
		}
		return key;
	}
	
	/**
	 * 获取16ID
	 * 
	 * @param conn
	 * @return
	 * @throws SQLException
	 */
	public String getSmallKeyId(Connection conn) throws SQLException
	{
		PreparedStatement pst = null;
		ResultSet rs = null;
		String key = "";
		try
		{
			pst = conn.prepareStatement("SELECT FN_GET_SYS_SMALL_KEY FROM DUAL");
			rs = pst.executeQuery();
			while (rs.next())
			{
				key = rs.getString(1);
			}
		}
		finally
		{
			try
			{
				if (rs != null)
				{
					rs.close();
				}
				if (pst != null)
				{
					pst.close();
				}
			}
			catch (SQLException e)
			{
			}
		}
		return key;
	}
}
